﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TankBattle.Models;
using System.IO;
using Microsoft.Xna.Framework;

namespace TankBattle.Models
{
    public class GameMap
    {
        #region Variable
        private List<List<int>> matrixMap;
        private bool error;

        #endregion 

        #region Properties
        public List<List<int>> MatrixMap
        {
            get { return matrixMap; }
        }
        public bool Error
        {
            get { return error; }
        }

        #endregion 

        #region Contruction
        public GameMap()
        {
            Initallization();
        }
        public GameMap(int level)
        {
            Initallization();
            ReadMap(level);
        }

        #endregion

        #region Function
        private void Initallization()
        {
            matrixMap = new List<List<int>>();
            error = false;

            for (int i = 0; i < 2*Constant.MAP_HEIGHT; i++)
            {
                matrixMap.Add(new List<int>());
                for (int j = 0; j < 2*Constant.MAP_WIDTH; j++)
                    matrixMap[i].Add('0');
            }
        }

        public void ReadMap(int level)
        {
            try
            {
                string[] strings = GetMap(level).Split('\n');
                if (strings == null) error = true;
                int i = -1, j = 0;

                foreach (string s in strings)
                {
                    i++;
                    j = -1;
                    string[] temp = s.Split(' ');
                    foreach (string ss in temp)
                    {
                        j++;
                        matrixMap[i][j] = int.Parse(ss);
                    }
                }
            }
            catch 
            {
                error = true;
            }
        }
        public void setObject(Vector2 position, int type)
        {
            matrixMap[(int)position.Y][(int)position.X] = type;
        }
        private string GetMap(int level)
        {
            switch (level)
            {
                case(1): return global::TankBattle.Resource.level1;
                case (2): return global::TankBattle.Resource.level2;
                default: return global::TankBattle.Resource.level1;
            }
        }

        #endregion
    }
}
